package top.drinyeit.weilong.presenter;

import java.util.List;

import top.drinyeit.weilong.activity.MyAddressActivity;
import top.drinyeit.weilong.adapter.AddressAdapter;
import top.drinyeit.weilong.app.Constant;
import top.drinyeit.weilong.model.AddressBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;
import top.drinyeit.weilong.util.SPUtil;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by bigHead on 2018/1/3.
 */

public class MyAddressPresenter {
    private MyAddressActivity activity;
    public AddressAdapter addressAdapter;

    public MyAddressPresenter(MyAddressActivity activity) {
        this.activity = activity;
    }

    //获取用户收货地址列表
    public void getAdd(final int pageIndex) {
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        activity.showLoading();
        CommonSubscriber<HttpResponse<List<AddressBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<AddressBean>>>() {
            @Override
            public void onError(String e, int code) {
                activity.shSwipeRefreshLayout.finishLoadmore();
                activity.shSwipeRefreshLayout.finishRefresh();
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<List<AddressBean>> o) {
                activity.shSwipeRefreshLayout.finishLoadmore();
                activity.shSwipeRefreshLayout.finishRefresh();
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    activity.pageIndex = pageIndex;
                    if (pageIndex == 1){
                        if (addressAdapter == null){
                            if (o.getData() != null && o.getData().size() > 0) {
                                addressAdapter = new AddressAdapter(activity, o.getData());
                                activity.addressListView.setAdapter(addressAdapter);
                            }
                        }else {
                            if (o.getData() == null || o.getData().size()  == 0) {
                                addressAdapter.clear();
                            }else {
                                addressAdapter.setData(o.getData());
                            }
                            addressAdapter.notifyDataSetChanged();
                        }
                    }else if (pageIndex > 1){
                        if (o.getData() != null && o.getData().size() > 0) {
                            addressAdapter.addData(o.getData());
                            addressAdapter.notifyDataSetChanged();
                        }else {
                            activity.showToastMsg("没有更多啦");
                        }
                    }
                }else {
                    if (pageIndex == 1 && addressAdapter != null){
                        addressAdapter.clear();
                        addressAdapter.notifyDataSetChanged();
                    }else {
                        if (addressAdapter != null){
                            activity.showToastMsg("没有更多了");
                        }else {
                            activity.showToastMsg(o.getMessage());
                        }
                    }
                }
            }
        });
        RetrofitMethod.getInstance().getAdd(subscriber, user_id, pageIndex);
    }

    //删除用户收货地址
    public void deleteAdd(int address_id) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS) {
                    getAdd(1);
                }

            }
        });
        RetrofitMethod.getInstance().deleteAdd(subscriber, address_id);
    }

    //设置用户默认收货地址
    public void setDe(int address_id, final int i) {
        String user_id = SPUtil.getShareString(Constant.USER_ID);
        activity.showLoading();
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                activity.dismissLoading();
                activity.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS) {
                    ((AddressAdapter) activity.addressListView.getAdapter()).isDe(i);
                    ((AddressAdapter) activity.addressListView.getAdapter()).notifyDataSetChanged();
                }
            }
        });
        RetrofitMethod.getInstance().setDe(subscriber, user_id, address_id);
    }

}
